<template>
  <div>
    <el-form ref="form" label-width="80px" :model="spu">
      <el-form-item label="SPU名称">
        <el-input placeholder="SPU名称" v-model="spu.spuName"></el-input>
      </el-form-item>
      <el-form-item label="品牌">
        <el-select placeholder="请选择品牌" v-model="spu.tmId">
          <el-option
            :label="tm.tmName"
            :value="tm.id"
            v-for="(tm,index) in TradeMarkList"
            :key="tm.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="SPU描述">
        <el-input type="textarea" rows="4" placeholder="描述" v-model="spu.description"></el-input>
      </el-form-item>
      <el-form-item label="SPU图片">
        <el-upload
          action="/dev-api/admin/product/fileUpload"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :file-list="SpuImageList"
          :on-success="handlersuccess"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt />
        </el-dialog>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-select :placeholder="`还有${unSelectSaleAttr.length}未选择`" v-model="attrId">
          <el-option
            :label="unselect.name"
            :value="`${unselect.id}:${unselect.name}`"
            v-for="(unselect,index) in unSelectSaleAttr"
            :key="unselect.id"
          ></el-option>
        </el-select>
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!attrId"
          @click="addSaleAttr"
        >添加销售属性</el-button>
        <el-table style="width: 100%" border :data="spu.spuSaleAttrList">
          <el-table-column type="index" label="序号" width="width"></el-table-column>
          <el-table-column prop="saleAttrName" label="属性名" width="width"></el-table-column>
          <el-table-column prop="prop" label="属性值名称列表" width="width">
            <template slot-scope="{row,$index}">
              <el-tag
                :key="tag.id"
                v-for="(tag,index) in row.spuSaleAttrValueList"
                closable
                :disable-transitions="false"
                @close="row.spuSaleAttrValueList.splice(index,1)"
              >{{tag.saleAttrValueName}}</el-tag>
              <el-input
                class="input-new-tag"
                v-if="row.inputVisible"
                v-model="row.inputValue"
                ref="saveTagInput"
                size="small"
                @blur="handleInputConfirm(row)"
              ></el-input>
              <el-button
                v-else
                class="button-new-tag"
                size="small"
                @click="addSaleAttrValue(row)"
              >添加</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row,$index}">
              <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-form-item>
          <el-button type="primary" @click="addOrUdateSpu">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Item from "@/layout/components/Sidebar/Item.vue";

export default {
  name: "",
  data() {
    return {
      dialogVisible: false,
      dialogImageUrl: "",

      spu: {
        category3Id: 0,
        description: "",
        spuName: "",
        tmId: "",
        spuImageList: [],

        spuSaleAttrList: [
          // {
          //   baseSaleAttrId: 0,
          //   id: 0,
          //   saleAttrName: "string",
          //   spuId: 0
          // }
        ]
      },
      TradeMarkList: [],
      SpuImageList: [],
      BaseSaleAttrList: [],
      attrId: ""
    };
  },
  methods: {
    handlersuccess(response, file, fileList) {
      this.spuImageList = fileList;
    },
    handleRemove(file, fileList) {
      this.spuImageList = fileList;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    async initSpuData(Spu) {
      console.log("发送请求", Spu);
      let spuresult = await this.$API.spu.reqSpu(Spu.id);
      if (spuresult.code == 200) {
        this.spu = spuresult.data;
      }
      let tradeMarkResult = await this.$API.spu.reqTradeMarkList();
      if (tradeMarkResult.code == 200) {
        this.TradeMarkList = tradeMarkResult.data;
      }
      let SpuImageList = await this.$API.spu.reqSpuImageList(Spu.id);
      if (SpuImageList.code == 200) {
        let listArr = SpuImageList.data;
        listArr.forEach(item => {
          item.name = item.imgName;
          item.url = item.imgUrl;
        });
        this.SpuImageList = listArr;
      }
      let BaseSaleAttrList = await this.$API.spu.reqBaseSaleAttrList();
      if (BaseSaleAttrList.code == 200) {
        this.BaseSaleAttrList = BaseSaleAttrList.data;
      }
    },
    addSaleAttr() {
      const [BaseSaleAttrId, saleAttrName] = this.attrId.split(":");
      let newSaleAttr = {
        BaseSaleAttrId,
        saleAttrName,
        spuSaleAttrValueList: []
      };
      this.spu.spuSaleAttrList.push(newSaleAttr);
      this.attrId = "";
    },
    addSaleAttrValue(row) {
      console.log("11");
      this.$set(row, "inputVisible", true);
      this.$set(row, "inputValue", "");
    },
    handleInputConfirm(row) {
      const { BaseSaleAttrId, inputValue } = row;
      if (inputValue.trim() == "") {
        this.$message("属性值不能为空");
      }
      let result = row.spuSaleAttrValueList.every(
        item => item.saleAttrValueName != inputValue
      );
      if (!result) return;
      let newSaleAttrValue = { BaseSaleAttrId, saleAttrValueName: inputValue };
      row.spuSaleAttrValueList.push(newSaleAttrValue);
      row.inputVisible = false;
    },
    async addOrUdateSpu() {
      this.spu.spuSaleAttrList = this.SpuImageList.map(item => {
        return {
          imgName: item.name,
          imgUrl: (item.response && item.response.data) || item.url
        };
      });
      let result = await this.$API.spu.reqAddOrUpdataSpu(this.spu);
      if (result.code == 200) {
        this.$message({ type: "success", message: "保存成功" });
        this.$emit("changeScene", {
          screen: 0,
          flag: this.spu.id ? "修改" : "添加"
        });
        Object.assign(this._data, this.$options.data());
      }
    },
    async addSpuData(category3Id) {
      this.spu.category3Id = category3Id;
      let tradeMarkResult = await this.$API.spu.reqTradeMarkList();
      if (tradeMarkResult.code == 200) {
        this.TradeMarkList = tradeMarkResult.data;
      }
      let BaseSaleAttrList = await this.$API.spu.reqBaseSaleAttrList();
      if (BaseSaleAttrList.code == 200) {
        this.BaseSaleAttrList = BaseSaleAttrList.data;
      }
    },
    cancel() {
      this.$emit("changeScene", { scene: 0, flag: "" });
      Object.assign(this._data, this.$options.data());
    }
  },
  computed: {
    unSelectSaleAttr() {
      let result = this.BaseSaleAttrList.filter(item => {
        return this.spu.spuSaleAttrList.every(item1 => {
          return item.name != item1.saleAttrName;
        });
      });
      return result;
    }
  }
};
</script>

<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>